package com.ebt.m.data;

import android.content.Context;
import android.database.sqlite.SQLiteException;

import com.ebt.m.config.DBOptionItem;
import com.ebt.m.config.DataBaseUpdateConfig;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.exception.EBTException.EBTException;

import java.util.List;

/**
 * 启动apk时更新数据库
 * 读取assets文件夹下DataBaseVersion.xml文件配置的数据库EbtDB节点、Log节点、HelperDB节点。
 */
public class DataBaseUpdate {

    private Context mContext = null;
    private static DatabaseManager dbManager;
//	private static LogDatabaseManager logDbManager;
//	private static DatabaseManager2Helper helperDbManager;

    public DataBaseUpdate(Context context) {
        mContext = context;
        dbManager = DatabaseManager.getInstance(mContext);
//		logDbManager = LogDatabaseManager.getInstance(mContext);
//		helperDbManager = DatabaseManager2Helper.getInstance(mContext);
    }

    public void updateEbtDataBase() throws EBTException {
        dbManager.open();
        dbManager.beginTransaction();
        try {
            int oldVersion = dbManager.getSqliteDatabase().getVersion();
//			new LogProvider(EBTInit.class).log("EbtoldVersion :" + oldVersion);
            String newVerionStr = DataBaseUpdateConfig.getEbtNewVersion();
//			new LogProvider(EBTInit.class).log("EbtNewVersion :" + newVerionStr);
            int newVersion = Integer.parseInt(newVerionStr);
            if (newVersion > oldVersion) {
                List<DBOptionItem> optionList = DataBaseUpdateConfig
                        .getEbtOptionsByVersion(oldVersion);
                if (optionList.size() > 0) {
                    for (DBOptionItem item : optionList) {
                        Logger.e(item.sql);
                        dbManager.executeSql(item.sql);

                    }
//					EventLogUtils.saveUserLog("EBT_DB_UPDATED");
                }
                dbManager.getSqliteDatabase().setVersion(newVersion);
            }
            dbManager.commit();
        } catch (SQLiteException e) {
            e.printStackTrace();
            dbManager.rollback();
//			new LogProvider(EBTInit.class).log("ebt version Update failed");
//			EventLogUtils.saveUserLog("EBT_DB_UPDATE_ERROR");
            throw new EBTException(e.toString());
        } catch (Exception e) {
            e.printStackTrace();
            dbManager.rollback();
//			new LogProvider(EBTInit.class).log("ebt version Update failed");
//			EventLogUtils.saveUserLog("EBT_DB_UPDATE_ERROR");
            throw new EBTException(e.toString());
        } finally {
            dbManager.close();
        }


    }

//	public void updateLogDataBase() throws EBTException {
//		logDbManager.open();
//		logDbManager.beginTransaction();
//		try {
//			int oldVersion = logDbManager.getSqliteDatabase().getVersion();
//			if (ConfigData.DEBUG) {
//				Log.d("DataBaseUpdateConfig ", "oldLogVersion :" + oldVersion);
//			}
//			String newVerionStr = DataBaseUpdateConfig.getLogNewVersion();
//			int newVersion = Integer.parseInt(newVerionStr);
//
//			if (newVersion > oldVersion) {
//				List<DBOptionItem> optionList = DataBaseUpdateConfig
//						.getLogOptionsByVersion(oldVersion);
//				if (optionList.size() > 0) {
//					for (DBOptionItem item : optionList) {
//						logDbManager.executeSql(item.sql);
//
//					}
//				}
//
//				logDbManager.getSqliteDatabase().setVersion(newVersion);
//			}
//			logDbManager.commit();
//		} catch (SQLiteException e) {
//			if (ConfigData.DEBUG) {
//				Log.d("DataBaseUpdate", e.toString());
//			}
//			logDbManager.rollback();
//			throw new EBTException(e.toString());
//		} catch (Exception e) {
//			if (ConfigData.DEBUG) {
//				Log.d("DataBaseUpdate", e.toString());
//			}
//			logDbManager.rollback();
//			throw new EBTException(e.toString());
//		} finally {
//			logDbManager.close();
//		}
//		if (ConfigData.DEBUG) {
//			Log.d("DataBaseUpdate", "DataBaseUpdate success");
//		}
//
//	}

//	public void updateHelperDataBase() throws EBTException {

//		int oldVersion = helperDbManager.getDbVersion();
//		if (ConfigData.DEBUG) {
//			Log.d("DataBaseHelperUpdateConfig ", "HelperOldLogVersion :" + oldVersion);
//		}
//		String newVerionStr = DataBaseUpdateConfig.getHelperNewVersion();
//		if (ConfigData.DEBUG) {
//			Log.d("DataBaseHelperUpdateConfig ", "HelperNewVerionStr :" + newVerionStr);
//		}
//		int newVersion = Integer.parseInt(newVerionStr);
//
//		if (newVersion > oldVersion) {
//			File databaseDir = new File(ConfigData.HELPERDATABASE);
//			databaseDir.delete();
//			EBTInit.copyHelperDB(mContext);
//			helperDbManager.setDbVersion(newVersion);
//		}
//
//		if (ConfigData.DEBUG) {
//			Log.d("DataBaseUpdate", "HelperDataBaseUpdate success");
//		}

//	}

}
